﻿using ERP.ERPSystem.ProductionSystem.Dto;
using ERP.Team.ProductionSystem;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace ERP.ERPSystem.ProductionSystem
{
    /// <summary>
    /// 生产系统-设备管理-维修单-xzc
    /// </summary>
    public class RepairOrdersServices: ApplicationService, IRepairOrdersServices
    {
        private readonly IRepository<Repair> _repairRepository;

        public RepairOrdersServices(IRepository<Repair> repairRepository)
        {
            _repairRepository = repairRepository;
        }
        /// <summary>
        /// 查询维修单
        /// </summary>
        /// <returns></returns>
        public async Task<List<Repair>> GetRepair(GetRepairDto getrepairdto)
        {
            var RepairList = await _repairRepository.GetListAsync();
            RepairList = RepairList.WhereIf(!string.IsNullOrEmpty(getrepairdto.RepairCode), m => m.RepairCode == getrepairdto.RepairCode).ToList();
            RepairList = RepairList.WhereIf(!string.IsNullOrEmpty(getrepairdto.RepairName), m => m.RepairName.Contains(getrepairdto.RepairName)).ToList();
            RepairList = RepairList.WhereIf(!string.IsNullOrEmpty(getrepairdto.DeviceCode), m => m.DeviceCode == getrepairdto.DeviceCode).ToList();
            RepairList = RepairList.WhereIf(!string.IsNullOrEmpty(getrepairdto.DeviceName), m => m.DeviceName.Contains(getrepairdto.DeviceName)).ToList();
            RepairList = RepairList.WhereIf(!string.IsNullOrEmpty(getrepairdto.RepairResults), m => m.RepairResults == getrepairdto.RepairResults).ToList();
            RepairList = RepairList.WhereIf(getrepairdto.DocumentStatus > 0, m => m.DocumentStatus == getrepairdto.DocumentStatus).ToList();
            
            // 使用维修单编号排序，保持数据位置稳定，修改后位置不变
            RepairList = RepairList.OrderBy(m => m.RepairCode).ToList();
            
            return RepairList;
        }
        /// <summary>
        /// 添加维修单
        /// </summary>
        /// <param name="repairdto"></param>
        /// <returns></returns>
        public async Task<int> AddRepair(RepairDto repairdto)
        {
            var i = ObjectMapper.Map<RepairDto, Repair>(repairdto);
            var repairList = await _repairRepository.InsertAsync(i);
            return repairList == null ? 0 : 1;
        }
        /// <summary>
        /// 更新维修单
        /// </summary>
        /// <param name="repairdto"></param>
        /// <returns></returns>
        public async Task<int> UpdateRepair(RepairDto repairdto)
        {
            var i = ObjectMapper.Map<RepairDto, Repair>(repairdto);
            var repairList = await _repairRepository.UpdateAsync(i);
            return repairList == null ? 0:1;
        }
        /// <summary>
        /// 通过id查询到相关维修单的数据
        /// </summary>
        /// <returns></returns>
        public async Task<List<Repair>> FindRepair(int id)
        {
            var RepairList = await _repairRepository.GetListAsync(m=>m.Id == id);
            return RepairList;
        }
        /// <summary>
        /// 删除维修单
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task DeleteRepair(int id)
        {
            await _repairRepository.DeleteAsync(m=>m.Id == id);
        }
        /// <summary>
        /// 批量删除维修单
        /// </summary>
        /// <param name="ids">要删除的维修单ID列表</param>
        /// <returns>删除成功的数量</returns>
        public async Task<int> BatchDeleteRepair(List<int> ids)
        {
            if (ids == null || ids.Count == 0)
            {
                return 0;
            }

            try
            {
                // 批量删除：使用Where条件一次性删除多个记录
                await _repairRepository.DeleteAsync(m => ids.Contains(m.Id));
                return ids.Count;
            }
            catch (Exception)
            {
                // 如果批量删除失败，尝试逐个删除并统计成功数量
                int successCount = 0;
                foreach (var id in ids)
                {
                    try
                    {
                        await _repairRepository.DeleteAsync(m => m.Id == id);
                        successCount++;
                    }
                    catch (Exception)
                    {
                        // 忽略个别删除失败的记录，继续删除其他记录
                        continue;
                    }
                }
                return successCount;
            }
        }
    }
}
